package cn.aitrox.ry.util;

import com.google.common.base.Strings;

public class NumParseUtil {
    public static Integer StringToInteger(String val) {
        if (Strings.isNullOrEmpty(val))
            return null;
        try {
            return Integer.valueOf(val);
        } catch (Exception e) {
            return null;
        }
    }

    public static Double StringToDouble(String val) {
        if (Strings.isNullOrEmpty(val))
            return null;
        try {
            return Double.valueOf(val);
        } catch (Exception e) {
            return null;
        }
    }

    public static Float StringToFloat(String val) {
        if (Strings.isNullOrEmpty(val))
            return null;
        try {
            return Float.valueOf(val);
        } catch (Exception e) {
            return null;
        }
    }

    public static Integer parseStringToNum(String val) {
        if (Strings.isNullOrEmpty(val) || Strings.isNullOrEmpty(val.trim())) {
            return 0;
        }
        String str = val.trim();// 去掉字符串首尾空格
        String ss = "";
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) >= 48 && str.charAt(i) <= 57) {
                ss += str.charAt(i);
            }
        }
        return Strings.isNullOrEmpty(ss) ? 0 : StringToInteger(ss);
    }

    public static Float parseFloat(Object val) {
        if (val == null)
            return null;

        return StringToFloat(String.valueOf(val));
    }

    public static String parseString(Object val) {
        if (val == null)
            return null;

        return String.valueOf(val);
    }

    public static Integer parseInt(Object val) {
        if (val == null)
            return null;

        return StringToInteger(String.valueOf(val));
    }

}
